// pages/bycard/bycard.js
var util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buying_image:'',
    fave_value:[],
    // amount: ['200', '300', '500', '800'],
    type: false ,
    now_value:'',
    buyid:'',
    defaultAddress: []
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      now_value:this.data.fave_value[e.detail.value].face_value,
      index: e.detail.value,
      // price: e.detail.value,      
      type: true
    })
  },
  chooseAddress: function () {
    app.navTo('../myads/myads?type=2' + "&isT=1")

  },
  formSubmit: function (e) {
    //console.log(e.detail.value);
    var buyid=this.data.buyid;
    var face_value = e.detail.value.face_value;
    var name = e.detail.value.name;
    var shouhuoaddress = this.data.defaultAddress.province + this.data.defaultAddress.city + this.data.defaultAddress.district + this.data.defaultAddress.address;
    if (face_value == '') {
      wx.showToast({
        title: '请选择面值',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (name == '') {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (shouhuoaddress == '') {
      wx.showToast({
        title: '请填写收货地址',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(true){
      var that = this;
      var param = {
        buying_id: buyid, face_value: face_value, name: name, receiving_address: shouhuoaddress 
      };
      util.commonRequest('buyingcard', 'post', param, function (res) {
        if (res.data.code == 200) {
           var id = res.data.data.id;
           var total = res.data.data.total;
          that.payMent(id, total);
        } else {
          wx.showToast({
            icon: 'loading',
            title: res.data.msg,
          })
        }
      });
    }

  },
  payMent:function(id,total){
    var that = this;
    var param = {
      body:"购买加油卡",
      total:total,
      id:id,
      jykType:1
    };
    util.commonRequest('XcuJyk', 'post', param, function (res) {
      if (res.data.code == 200) {
        wx.requestPayment({
          'timeStamp': '' + res.data.data.timeStamp + '',
          'nonceStr': '' + res.data.data.nonceStr + '',
          'package': '' + res.data.data.package + '',
          'signType': 'MD5',
          'paySign': '' + res.data.data.paySign + '',
          success: function (res) {
            console.log(res)
            wx.showToast({
              title: '支付成功',
              icon: 'loading'
            })
            // handler.intoPageHandler("../user-appointment-record/index");
          },
          fail: function (res) {
            wx.showToast({
              title: '支付失败',
              icon: 'loading'
            })
          }
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },
  nameInputEvent: function (e) {
    var name = e.detail.value;
    console.log(name);
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options.buyid);
    var buyid = options.buyid;
    this.setData({
      buyid: buyid
    })

   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    util.commonRequest('buyImg', 'post', { buyid: that.data.buyid }, function (res) {
      if (res.data.code == 200) {
        that.setData({
          buying_image: res.data.data,
          fave_value: res.data.data.faceValue,
          defaultAddress: res.data.data.defaultAddress
        });
        //console.log(res.data.data.buying_image)
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})